from dvadmin.utils.json_response import ErrorResponse, DetailResponse
from rest_framework.views import APIView
from dvadmin.utils.viewset import CustomModelViewSet
from rest_framework.decorators import action
from dvadmin.utils.serializers import CustomModelSerializer
from rest_framework import serializers
from threading import Timer


class ApiWeChatLoginSerializer(CustomModelSerializer):
    # 微信用户扫码登录设备传递参数
    open_id = serializers.CharField()
    device_id = serializers.CharField()


class WeChatLoginView(APIView):

    serializer_class = ApiWeChatLoginSerializer
    authentication_classes = []
    permission_classes = []


    def post(self, request):
        open_id = request.data.get("open_id")
        device_id = request.data.get("device_id")

        login_success = self.check_login(open_id)

        if(login_success):
            return DetailResponse(data={
                "msg":"登录成功"
            })
        else:
            return ErrorResponse(msg="登录失败,没有此用户")

    def check_login(self,open_id):
        # 如果数据库中有这个用户，则登录成功，没有则提示在小程序中注册
        return True
    